#include <QtCore/QCoreApplication>
#include <QByteArray>
#include <QDebug>
//#include <QScriptEngine>
#include <qpcap.h>
//#include <stdInReader.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QPcap pcap;
    stdInReader c;
    //c.init();
    c.open();
    //connect(QSEngine,SIGNAL(signalHandlerException(const QScriptValue &)),this,SLOT(signalHandlerException_rcv( const QScriptValue &)));
    //c=new stdInReader;
    //QObject::connect(&c, SIGNAL(received_StdIn()), &c, SLOT(send_StdOut()));
    //QString device = pcap.lookupDevice();//идентификации сетевого интерфейса. Возврощает имя подходящего интерфейса (device) или описание ошибки в буфер errbuf
    QString device = pcap.findallDevices(&c);
    c.send_StdOut(QByteArray().append(QString("ок")));
    /*    if ( device.isEmpty() ) {
        qDebug() << "Lookup device failed, " << pcap.errorString();
        return 1;
    }
    else {
        qDebug() << "Lookup device=" << device;
    }*/
    bool ok;
    ok = pcap.open( device, 65535, true );
    if (!ok) {
        qDebug() << "Unable to open, " << pcap.errorString();
        return 1;
    }
    ok = pcap.setFilter( QString("bad filter") );
    if (!ok) {
        qDebug() << "bad filter failed (good!), " << pcap.errorString();
    }
#if 1
    ok = pcap.setFilter( QString("host xmelegance.org and port 80") );
    if (!ok) {
        qDebug() << "filter failed, " << pcap.errorString();
    }
#endif
    for (int i=0; i < 3; i++ ) {
        ok = pcap.readPacket();
        if (!ok) {
            qDebug() << "Failed to read a packet, " << pcap.errorString();
            //return 1;
        }
        QPcapHeader header = pcap.header();
        qDebug() << "Got one packet, length is " << header.packetLength() << "captured " << header.capturedLength();
        const u_char *packet = pcap.packet();
        QByteArray bytes( (const char *)packet, header.capturedLength() );
        qDebug() << bytes.toHex();
    }
    pcap.start();
    pcap.close();
    a.exec();
}
